package com.ywjt.platformbusiness.message;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.common.UserData;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.message.adapter.MessageAdapter;
import com.ywjt.platformbusiness.store.adapter.BusinessCenterMessageAdapter;
import com.ywjt.platformbusiness.store.model.BusinessHomeInfoModel;
import com.ywjt.platformbusiness.store.model.BusinessMessageModel;
import com.ywjt.platformbusiness.util.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class BusinessMessageActivity extends BaseActivity {
    private RecyclerView  rcCommon;
    private SmartRefreshLayout refresh;
    private BusinessCenterMessageAdapter adapter;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, BusinessMessageActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_common;
    }

    @Override
    protected void initView() {
        setTitleText("消息通知");
        rcCommon=findViewById(R.id.rcCommon);
        refresh=findViewById(R.id.refresh);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getMessageData();
            }
        });
        getMessageData();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }


    private void getMessageData() {
        new HttpRequest(getContext()).doPost(UrlConstants.merchant_getMessageNotice+ UserData.create(this).get(UserData.MERCHANTID), "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        refresh.finishRefresh();
                        refresh.finishRefreshWithNoMoreData();
                        Object obj = JsonUtils.fromJson(result.toString(), BusinessMessageModel.class);
                        if (obj instanceof BusinessMessageModel) {
                            BusinessMessageModel model = (BusinessMessageModel) obj;
                            adapter=new BusinessCenterMessageAdapter(model.getResult());
                            rcCommon.setLayoutManager(new LinearLayoutManager(BusinessMessageActivity.this));
                            rcCommon.setAdapter(adapter);
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }
}
